<?php


namespace app\mall\controller\Order\Goods;


use app\mall\controller\AppController;
use app\mall\controller\BaseController;
use app\mall\model\Order\OrderGoodsModel;
use app\mall\model\Order\OrderModel;
use app\mall\model\System\LogisticsModel;
use app\Request;
use app\Utils\ImageUtil;
use app\Utils\Util;

class GoodsOrder extends AppController
{

    /**
     * 分页查询
     * @param Request $request
     * @return array
     */
    public function getOrderGoodsList(Request $request)
    {
        $param = $request->param();
        $where = [];
        $page = !empty($param['page']) ? $param['page'] : 1;
        $limit = !empty($param['limit']) ? $param['limit'] : 10;
        $status = !empty($param['status']) ? $param['status'] : 0;
        array_push($where,['status','=',$status]);
        array_push($where,['mer_id','=',$this->mall['id']]); //商家ID
        if(!empty($param['is_group'])){
            if($param['is_group']==1){
                array_push($where,['is_group','=',1]);
            }else if($param['is_group']==2){
                array_push($where,['is_group','=',0]);
            }
        }
        $list = OrderModel::getOrderListWhere($page, $limit, $where);
        $count = OrderModel::getOrderCountWhere($where);
        foreach ($list as &$item){
            foreach ($item['list'] as &$val){
                $goods_info = json_decode($val['goods_info'],true);
                $val['goods_info'] = $goods_info;
                if($val['type'] == 1){//商品
                    $data = [
                        'name'           => $goods_info['good_name'],
                        'img'            => ImageUtil::i()->replaceUrl($goods_info['img']),
                        'number'         => $val['number'],
                        'slider_img'     => $goods_info['slider_img'],
                        'price'          => $val['goods_price'],
                        'total_price'    => $val['total_price']
                    ];
                }
                if($val['type'] == 2){//会员
                    $data = [
                        'name'         => $goods_info['level_name'],
                        'img'          => ImageUtil::i()->replaceUrl($goods_info['icon']),
                        'number'       => $val['number'],
                        'slider_img'   => "",
                        'price'        => $val['goods_price'],
                        'total_price'  => $val['total_price']
                    ];
                }
                if($val['type'] == 3){//课程
                    $data = [
                        'name'         => $goods_info['title'],
                        'img'          => ImageUtil::i()->replaceUrl($goods_info['img']),
                        'number'       => $val['number'],
                        'slider_img'   => $goods_info['slider_img'],
                        'price'        => $val['goods_price'],
                        'total_price'  => $val['total_price']
                    ];
                }
                $val['info'] = !empty($data) ? $data : [];
            }
            unset($val);
        }
        return Util::returnPageJson(self::CODE_DATA, $count, $list, "");
    }



    /**
     * 发货
     * @param Request $request
     */
    public function deliverOrder(Request $request)
    {
        $param = $request->param();
        if(!empty($param['goods_order_id'])){
            foreach ($param['goods_order_id'] as $key=>$id){
                $logistic = LogisticsModel::getLogisticsWhere([['com','=',$param['lgoistics_com'][$key]]]);
                $save = [
                    'status'        => 2,
                    'lgoistics_com' => $param['lgoistics_com'][$key],
                    'lgoistics_num' => $param['lgoistics_num'][$key],
                    'lgoistics_name'=> $logistic['company_name'],
                    'up_time'       => date("Y-m-d H:i:s",time())
                ];
                OrderGoodsModel::updateOrderGoodsWhere([['id','=',$id]],$save);
            }
        }
        $order_save = [
            'status'  => 2,
            'up_time' => date("Y-m-d H:i:s",time())
        ];
        OrderModel::updateOrderWhere([['id','=',$param['order_id']]],$order_save);
        return Util::returnJson(self::CODE_SUCCESS,null,"发货成功");
    }

}